﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIGroupTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetExpanderStateForExpander()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIGroup expander = window.UIGroup("@AutomationId='expander1'");
            Assert.IsFalse(expander.IsExpanded);
        }

        [TestMethod]
        public void ShouldGetDefaultExpanderStateUnsupportedGroupControl()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIGroup groupBox = window.UIGroup("@AutomationId='groupBox1'");
            Assert.IsFalse(groupBox.IsExpanded);
        }

        [TestMethod]
        public void ShouldExpandExpander()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIGroup expander = window.UIGroup("@AutomationId='expander1'");
            Assert.IsFalse(expander.IsExpanded);
            expander.Expand();
            Assert.IsTrue(expander.IsExpanded);

            expander.Expand();
            Assert.IsTrue(expander.IsExpanded);
        }

        [TestMethod]
        public void ShouldCollapseExpander()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIGroup expander = window.UIGroup("@AutomationId='expander1'");
            expander.Expand();
            Assert.IsTrue(expander.IsExpanded);
            expander.Collapse();
            Assert.IsFalse(expander.IsExpanded);

            expander.Collapse();
            Assert.IsFalse(expander.IsExpanded);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))] 
        public void ShouldThrowExceptionWhenExpandingGroupControl()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIGroup groupBox = window.UIGroup("@AutomationId='groupBox1'");
            groupBox.Expand();
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))] 
        public void ShouldThrowExceptionWhenCollapsingGroupControl()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIGroup groupBox = window.UIGroup("@AutomationId='groupBox1'");
            groupBox.Collapse();
        }

        [TestMethod]
        public void ShouldGetAllGroupControls()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            List<UIGroup> groupControls = window.UIGroups();
            Assert.AreEqual(2, groupControls.Count);
            Assert.AreEqual("Expander", groupControls[0].Name);
            Assert.AreEqual("Group Control", groupControls[1].Name);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}